//
//  TOP.m
//  Project1
//
//  Created by 黄珂耀 on 15/7/17.
//  Copyright (c) 2015年 黄珂耀. All rights reserved.
//

#import "TOP.h"
#import "jsonAnalysis.h"
#import "TopModal.h"
#import "common.h"
#import "TopCell.h"

@interface TOP ()

@end

@implementation TOP

#pragma mark - DefaultOperation

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.title = @"Top250";
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initWithData];
    [self _loadData];
    [self _createSubviews];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Init

- (void)initWithData
{
    _topModalArray = [[NSMutableArray alloc] init];
}

#pragma mark - LoadData

- (void)_loadData
{
    NSDictionary *rootDic = [jsonAnalysis initWithJsonFileName:@"top250.json"];
    
    NSArray *subjects = [rootDic objectForKey:@"subjects"];
    
    for (NSDictionary *dic in subjects)
    {
        TopModal *modal = [[TopModal alloc] init];
//        modal.title = [dic objectForKey:@"title"];
//        modal.images = [dic objectForKey:@"images"];
//        NSNumber *number = [[dic objectForKey:@"rating"] objectForKey:@"average"];
//        modal.average = [number floatValue];
        
        [modal setValuesForKeysWithDictionary:dic];
        
        [_topModalArray addObject:modal];
    }
}

#pragma mark - CreateSubviews

- (void)_createSubviews
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    layout.itemSize = CGSizeMake( KWidth / 3, 180);
    
    _topCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight) collectionViewLayout:layout];
    [_topCollectionView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]]];
    
    _topCollectionView.delegate = self;
    _topCollectionView.dataSource = self;
    
    UINib *nib = [UINib nibWithNibName:@"TopCell" bundle:[NSBundle mainBundle]];
    
    [_topCollectionView registerNib:nib forCellWithReuseIdentifier:@"topCell"];
    
    [self.view addSubview:_topCollectionView];
}

#pragma mark - TopCollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_topModalArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topCell" forIndexPath:indexPath];
    
    cell.modal = _topModalArray[indexPath.row];
    
    return cell;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
